﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Gencode.Web.Common
{
    public class gUtil
    {
        #region enum
        public enum PathType
        {
            Physical,
            Virtual
        }
        #endregion

        public static string GetVirtualPath(string path)
        {
            return GetVirtualPath(path, true);
        }

        public static string GetVirtualPath(string path, bool languageDepending)
        {
            if (InDesign)
                return path;

            if (path == "" || path[0] != '/')
                return path;

            string applicationPath = HttpContext.Current.Request.ApplicationPath;
            if ((applicationPath == "" || applicationPath == "/") && !gConfig.IsRootApp)
                return path;
            else
            {
                string lanDir = "";// GetLanguageDir();

                string result = String.Empty;
                if (!gConfig.IsRootApp)
                    result = languageDepending ?
                        String.Format("{0}{1}{2}{3}",
                        applicationPath,
                        lanDir == String.Empty ? String.Empty : @"/",
                        lanDir == String.Empty ? lanDir : lanDir.Substring(0, lanDir.LastIndexOf('/')),
                        path
                        )
                        :
                        applicationPath + path;
                else
                    result = languageDepending ?
                        String.Format("{0}{1}{2}",
                        lanDir == String.Empty ? lanDir : applicationPath,
                        lanDir == String.Empty ? lanDir : lanDir.Substring(0, lanDir.LastIndexOf('/')),
                        path
                        )
                        :
                        path;

                return result;
            }
        }

        public static string GetPhysicalApplicationPath()
        {
            return HttpContext.Current.Request.PhysicalApplicationPath;
        }

        //public static string GetLanguageDir()
        //{
        //    return GetLanguageDir(PathType.Virtual);
        //}

        //public static string GetLanguageDir(PathType path)
        //{
        //    if (Config.CultureId == cmConfig.CultureIdentifier.DE)
        //        return String.Empty;
        //    else
        //    {
        //        string slash = String.Empty;
        //        switch (path)
        //        {
        //            case PathType.Physical:
        //                slash = @"\";
        //                break;
        //            case PathType.Virtual:
        //                slash = @"/";
        //                break;
        //            default:
        //                throw new Exception("Unsupported enum");
        //        }
        //        return cmConfig.CultureId.ToString() + slash;
        //    }
        //}

        #region properties
        public static bool InDesign
        {
            get
            {
                return HttpContext.Current == null;
            }
        }
        #endregion
    }
}
